from django import shortcuts
from google.appengine.api import users
from plzkeepthat.common import constants as app
from plzkeepthat.common.util import get_notelist
from plzkeepthat.models.util.dbutils import get_all_tags
from django.http import HttpResponse

class Messages:
    """ Class that manages messages to the user. """
    messages = []
    
    @classmethod
    def clear(self):
        """ Empties the list of messages. """
        Messages.messages = []
        
    @classmethod
    def add(self, message):
        """ Adds a message to the list. """
        Messages.messages.append(message)
        
        
def respond(template, params = {}):
    """ Renders a template with the given parameters to a response. """
    srcName = template
    if not template.endswith(app.TEMPLATE_EXT):
        template += app.TEMPLATE_EXT

    params["title"] = app.TITLE
    params["author"] = app.AUTHOR
    params["year"] = app.YEAR
    params["src"] = srcName
    params["logout"] = users.create_logout_url("/")
    params["tags"] = get_all_tags(get_notelist(users.get_current_user()))
    params["messages"] = Messages.messages
    Messages.clear()
    
    return shortcuts.render_to_response(template, params)

def raw_response(data, mimetype):
    """ 
    Creates a response with the given mimetype and data. Could be used
    for binary data.
    """
    return HttpResponse(data, mimetype = mimetype)